
package com.abcijkxyz.dialects.stsm.score;

import java.util.Calendar;
import java.util.List;

import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.exceptions.TemplateProcessingException;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.model.IOpenElementTag;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractAttributeModelProcessor;
import org.thymeleaf.processor.element.IElementModelStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;

public class MatchDayTodayModelProcessor extends AbstractAttributeModelProcessor {

	private static final String	ATTR_NAME	= "match-day-today";
	private static final int	PRECEDENCE	= 100;

	public MatchDayTodayModelProcessor(final String dialectPrefix) {
		super(TemplateMode.HTML, // This processor will apply only to HTML mode
				dialectPrefix, // Prefix to be applied to name for matching
				null, // No tag name: match any tag name
				false, // No prefix to be applied to tag name
				ATTR_NAME, // Name of the attribute that will be matched
				true, // Apply dialect prefix to attribute name
				PRECEDENCE, // Precedence (inside dialect's own precedence)
				true); // Remove the matched attribute afterwards
	}

	protected void doProcess(final ITemplateContext context, final IModel model, final AttributeName attributeName, final String attributeValue, final IElementModelStructureHandler structureHandler) {

		if (!checkPositionInMarkup(context)) {
			throw new TemplateProcessingException("The " + ATTR_NAME + " attribute can only be used inside a " + "markup element with class \"leaguetable\"");
		}

		final Calendar now = Calendar.getInstance(context.getLocale());
		final int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);

		// Sundays are Match Days!!
		if (dayOfWeek == Calendar.SUNDAY) {

			// The Model Factory will allow us to create new events
			final IModelFactory modelFactory = context.getModelFactory();

			// We will be adding the "Today is Match Day" banner just after
			// the element we are processing for:
			//
			// <h4 class="matchday">Today is MATCH DAY!</h4>
			//
			model.add(modelFactory.createOpenElementTag("h4", "class", "matchday")); //
			model.add(modelFactory.createText("Today is MATCH DAY!"));
			model.add(modelFactory.createCloseElementTag("h4"));

		}

	}

	private static boolean checkPositionInMarkup(final ITemplateContext context) {

		/*
		 * We want to make sure this processor is being applied inside a container tag which has class="leaguetable". So we need to check the second-to-last entry in the element stack (the last entry is the tag being processed itself).
		 */

		final List<IProcessableElementTag> elementStack = context.getElementStack();
		if (elementStack.size() < 2) {
			return false;
		}

		final IProcessableElementTag container = elementStack.get(elementStack.size() - 2);
		if (!(container instanceof IOpenElementTag)) {
			return false;
		}

		final String classValue = container.getAttributeValue("class");
		return classValue != null && classValue.equals("leaguetable");

	}

}
